package com.qinhao.loveCrowdFunding.service;

import com.qinhao.loveCrowdFunding.mapper.PayMapper;
import com.qinhao.loveCrowdFunding.req.PayRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class PayService {
    @Autowired
    PayMapper payMapper;

    @Transactional
    public void addPay(PayRequest payRequest) {
        payMapper.addPayToDonate(payRequest);
        if (payMapper.getHasDonated(payRequest) == null ) {
            payMapper.updateDonateBySimpleInformationId(payRequest);
        } else {
            Integer alreadyFundingCount = Integer.parseInt(payMapper.getHasDonated(payRequest).getAlreadyFundingCount());
            alreadyFundingCount += payRequest.getDonateCount();
            payRequest.setDonateCount(alreadyFundingCount);
            payMapper.updateDonateBySimpleInformationId(payRequest);
        }
    }
}
